package com.example.jiaowuchu.jiaowuSys.dao;
import com.example.jiaowuchu.jiaowuSys.pojo.yhpojo.User;
import com.example.jiaowuchu.jiaowuSys.util.DBUtil;
import com.example.jiaowuchu.jiaowuSys.pojo.yhpojo.Student;
import com.example.jiaowuchu.jiaowuSys.pojo.kcpojo.course;
import com.example.jiaowuchu.jiaowuSys.pojo.kcpojo.exam;
import com.example.jiaowuchu.jiaowuSys.pojo.kcpojo.xk;

import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentDao {
    private ArrayList list;

    /**
     * 查询本人的学生信息
     */
    public Student selectStudent(String xh) {
        try(Connection conn = DBUtil.getConnection()){
            String sql = "select * from Student where xh = ? ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,xh);
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                String xh1 = rs.getString("xh");
                String xsm = rs.getString("xsm");
                String xsxy = rs.getString("xsxy");
                Integer xsnl = rs.getInt("xsnl");
                String xsxb = rs.getString("xsxb");
                String xsxf = rs.getString("xsxf");
                String xsqq  = rs.getString("xsqq");
                String xssjh = rs.getString("xssjh");
                String xsjg = rs.getString("xsjg");
                return new Student(xh1,xsm,xsxy,xsnl,xsxb,xsxf,xsqq,xssjh,xsjg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     *  查询全部课程
     */
    public ArrayList<course> selectallcourse() {
        try(Connection conn = DBUtil.getConnection()){
            String sql = "select * from course";
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                String kch = rs.getString("kch");
                String kcm = rs.getString("kcm");
                String jsh = rs.getString("jsh");
                String kcxf = rs.getString("kcxf");
                String kcxz = rs.getString("kcxz");
                String kcsj = rs.getString("kcsj");

                list.add(new course(kch,kcm,jsh,kcxf,kcxz,kcsj));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     *  查询本学生已选的全部课程
     */
    public ArrayList<course> selectallcourse(String  xh) {
        try(Connection conn = DBUtil.getConnection()){
            String sql = "select * from course where kch = (select kch from studentcourse where xh = ? );";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,xh);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                String kch = rs.getString("kch");
                String kcm = rs.getString("kcm");
                String jsh = rs.getString("jsh");
                String kcxf = rs.getString("kcxf");
                String kcxz = rs.getString("kcxz");
                String kcsj = rs.getString("kcsj");

                list.add(new course(kch,kcm,jsh,kcxf,kcxz,kcsj));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     *  查询本学生所有课程成绩
     */
    public ArrayList<course> selectallscore(String  xh) {
        try(Connection conn = DBUtil.getConnection()){
            String sql = "select * from studentcourse where xh = ?;";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,xh);
            ResultSet rs = stmt.executeQuery();
            while(rs.next()){
                String kch = rs.getString("kch");
                String kcm = rs.getString("kcm");
                String kcxf = rs.getString("kcxf");
                String kcxz = rs.getString("kcxz");
                String kccj = rs.getString("kccj");

                list.add(new course(kch,kcm,kcxf,kcxz,kccj));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }


    /**
     *  查询本学生已报名课程的考试安排
     */
    public ArrayList<exam> selectexam(String  xh) {
        try(Connection conn = DBUtil.getConnection()){
            String sql = "select a.* from exam a ,studentcourse b where  a.kch =b.kch and xh = ?;";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,xh);
            ResultSet rs = stmt.executeQuery();
            if(rs.next()){
                String ksm = rs.getString("ksm");
                String kssj = rs.getString("kssj");
                String ksdd = rs.getString("ksdd");
                String kssc = rs.getString("kssc");
                String kszt = rs.getString("kszt");
                String kch = rs.getString("kch");

                list.add( new exam(ksm,kssj,ksdd,kssc,kszt,kch) );
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    /*
    选课中使用的方法，用于查找单个课程
     */

    public course listcourse(String kch) {
        Throwable var3 = null;
        try {
            Connection conn = DBUtil.getConnection();
            try {
                String sql = "select * from course where kch = ?";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1,kch);
                ResultSet rs = stmt.executeQuery();
                while(rs.next()) {
                    String kch1 = rs.getString("kch");
                    String kcm = rs.getString("kcm");
                    String jsh = rs.getString("jsh");
                    String kcxf = rs.getString("kcxf");
                    String kcxz = rs.getString("kcxz");
                    String kcsj = rs.getString("kcsj");

                    return new course(kch, kcm, jsh, kcxf, kcxz, kcsj);
                }
            } catch (Throwable var25) {
                var3 = var25;
                throw var25;
            } finally {
                if (conn != null) {
                    if (var3 != null) {
                        try {
                            conn.close();
                        } catch (Throwable var24) {
                            var3.addSuppressed(var24);
                        }
                    } else {
                        conn.close();
                    }
                }

            }
        } catch (Exception var27) {
            var27.printStackTrace();
        }

        return null;
    }

    /**
     *  选课
     */
    public boolean chosecourse(String kch,String xh){
        try {
            Connection conn = DBUtil.getConnection();
            Throwable var3 = null;
            boolean var6;
            course course1 = listcourse(kch);
            try {
                String sql = "insert into studentcourse(xh,kch,kcm,jsh,kcxz) VALUES (?,?,?,?,?)";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1, xh);
                stmt.setString(2, kch);
                stmt.setString(3, course1.getKcm());
                stmt.setString(4, course1.getJsh());
                stmt.setString(5, course1.getKcxz());
                var6 = stmt.executeUpdate() > 0;
            } catch (Throwable var16) {
                var3 = var16;
                throw var16;
            } finally {
                if (conn != null) {
                    if (var3 != null) {
                        try {
                            conn.close();
                        } catch (Throwable var15) {
                            var3.addSuppressed(var15);
                        }
                    } else {
                        conn.close();
                    }
                }

            }

            return var6;
        } catch (Exception var18) {
            var18.printStackTrace();
            return false;
        }
    }




    /**
     *  退课
     */
    public boolean deletecourse(String kch,String xh) {
        try {
            Connection conn = DBUtil.getConnection();
            Throwable var3 = null;

            boolean var6;
            try {
                String sql = "delete from studengcourse where kch = ? and xh = ? ";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1, kch);
                stmt.setString(2, xh);
                var6 = stmt.executeUpdate() > 0;
            } catch (Throwable var16) {
                var3 = var16;
                throw var16;
            } finally {
                if (conn != null) {
                    if (var3 != null) {
                        try {
                            conn.close();
                        } catch (Throwable var15) {
                            var3.addSuppressed(var15);
                        }
                    } else {
                        conn.close();
                    }
                }

            }

            return var6;
        } catch (Exception var18) {
            var18.printStackTrace();
            return false;
        }
    }
    /**
     *  教学评估
     */
    public boolean jiaoxuepinggu(String score,String kch,String jsh,String xh) {
        try {
            Connection conn = DBUtil.getConnection();
            Throwable var3 = null;
            boolean var6;
            course course1 = listcourse(kch);
            try {
                String sql = "insert into jxpgcjb(jsh,xh,kch,jscj) VALUES (?,?,?,?)";
                PreparedStatement stmt = conn.prepareStatement(sql);
                stmt.setString(1, jsh);
                stmt.setString(2, xh);
                stmt.setString(3, kch);
                stmt.setString(4, score);
                var6 = stmt.executeUpdate() > 0;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            return false;
        }

    }
}
